package com.wing.visual.service;

import cn.hutool.core.util.ObjectUtil;
import com.wing.common.utils.DateUtils;
import com.wing.visual.dao.PurchaseDao;
import com.wing.visual.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 采购可视化
 *
 * @author xiayang
 */
@Service
@Slf4j
public class PurchaseServiceImpl implements PurchaseService {

    @Resource
    PurchaseDao purchaseDao;

    @Override
    public Map<String, Object> purchaseTotal(Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        //年度采购总金额
        BigDecimal yearPurchaseAmount = purchaseDao.purchaseAmountTotalByYear();
        result.put("yearPurchaseAmount", yearPurchaseAmount);
        //月度采购总金额
        BigDecimal monthPurchaseAmount = purchaseDao.purchaseAmountTotalByMonth();
        result.put("monthPurchaseAmount", monthPurchaseAmount);
        //新增采购数量
        Integer addPurchaseNum = purchaseDao.addPurchaseNumTotal(params);
        result.put("addPurchaseNum", addPurchaseNum);
        //签收金额
        BigDecimal signAmount = purchaseDao.signAmountTotal(params);
        result.put("signAmount", signAmount);
        //新增采购订单数
        Integer addPurchaseOrderNum = purchaseDao.addPurchaseOrderNumTotal(params);
        result.put("addPurchaseOrderNum", addPurchaseOrderNum);
        //签收物料数量
        Integer signMaterialNum = purchaseDao.signMaterialNumTotal(params);
        result.put("signMaterialNum", signMaterialNum);
        return result;
    }

    @Override
    public List<PurchaseOrderRate> purchaseOrderRate(Map<String, Object> params) {
        //订单进度-采购数量
        List<PurchaseOrderRate> purchaseList = purchaseDao.purchaseOrderRateByPurchase(params);
        //订单进度-签收数量
        List<PurchaseOrderRate> signList = purchaseDao.purchaseOrderRateBySign(params);
        for (PurchaseOrderRate purchaseOrderRate : purchaseList) {
            PurchaseOrderRate sign = signList.stream().filter(t -> t.getPurchaseOrderSn().equals(purchaseOrderRate.getPurchaseOrderSn())).findFirst().orElse(null);
            if (sign != null && sign.getPurchaseOrderSignNum() != null) {
                purchaseOrderRate.setPurchaseOrderSignNum(sign.getPurchaseOrderSignNum());
                purchaseOrderRate.setSignRate(new BigDecimal(purchaseOrderRate.getPurchaseOrderSignNum()).divide(new BigDecimal(purchaseOrderRate.getPurchaseOrderQuantity()),4, BigDecimal.ROUND_HALF_UP));
            } else {
                purchaseOrderRate.setPurchaseOrderSignNum(0);
                purchaseOrderRate.setSignRate(BigDecimal.ZERO);
            }
        }
        purchaseList.sort(new Comparator<PurchaseOrderRate>() {
            @Override
            public int compare(PurchaseOrderRate o1, PurchaseOrderRate o2) {
                return Integer.compare(o1.getSignRate().intValue(), o2.getSignRate().intValue());
            }
        });
        return purchaseList;
    }

    @Override
    public List<PurchaseTypeAmountTotal> purchaseOrderTypeAmountTotal(Map<String, Object> params) {
        return purchaseDao.purchaseOrderTypeAmountTotal(params);
    }

    @Override
    public List<SupplierPurchaseAmountTotal> supplierPurchaseAmountTotal(Map<String, Object> params) {
        return purchaseDao.supplierPurchaseAmountTotal(params);
    }

    @Override
    public List<PurchaseOrderAmount> purchaseOrderAmountGraph() {
        List<PurchaseOrderAmount> list = new ArrayList<>();
        //采购金额列表
        List<PurchaseOrderAmount> purchaseAmountList = purchaseDao.purchaseOrderAmountGraph();
        //时间间隔天数
        String startTime = DateUtils.format(DateUtils.getStartOfMonth(new Date()), DateUtils.DATE_PATTERN);
        String endTime = DateUtils.format(DateUtils.getEndOfMonth(new Date()), DateUtils.DATE_PATTERN);
        Integer dayNum = DateUtils.getTimeDay(startTime, endTime);
        for (int i = dayNum; i >= 0; i--) {
            String purchaseDate = DateUtils.getDateByDay(DateUtils.stringFormatDate(endTime, DateUtils.DATE_PATTERN), i == 0 ? 0 : -i);
            PurchaseOrderAmount purchaseOrderAmount = purchaseAmountList.stream().filter(t -> t.getPurchaseDate().equals(purchaseDate)).findFirst().orElse(null);
            if(purchaseOrderAmount == null){
                purchaseOrderAmount =  new PurchaseOrderAmount();
                purchaseOrderAmount.setPurchaseDate(purchaseDate);
                purchaseOrderAmount.setPurchaseAmount(BigDecimal.ZERO);
            }
            list.add(purchaseOrderAmount);
        }
        return list;
    }

    @Override
    public List<PurchaseMaterialTotal> materialTotal(Map<String, Object> params) {
        //采购订单物料统计-采购量
        List<PurchaseMaterialTotal> purchaseNumList = purchaseDao.materialTotalByPurchaseNum(params);
        //采购订单物料统计-签收量
        List<PurchaseMaterialTotal> signNumList = purchaseDao.materialTotalBySignNum(params);
        for (PurchaseMaterialTotal purchaseMaterialTotal : purchaseNumList) {
           if (ObjectUtil.isNotEmpty(purchaseMaterialTotal.getSkuId())){
               PurchaseMaterialTotal sign = signNumList.stream()
                       .filter(t-> t.getSkuId()!=null && t.getSkuId().longValue() == purchaseMaterialTotal.getSkuId().longValue())
                       .findFirst()
                       .orElse(null);
               if(sign != null && sign.getPurchaseSignNum() != null){
                   purchaseMaterialTotal.setPurchaseSignNum(sign.getPurchaseSignNum());
               }else{
                   purchaseMaterialTotal.setPurchaseSignNum(0);
               }
           }
        }
        return purchaseNumList;
    }
}
